package com.example.another.bottomnavigationdemo1;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.another.bottomnavigationdemo1.fragment.FirstFragment;
import com.example.another.bottomnavigationdemo1.fragment.FiveFragment;
import com.example.another.bottomnavigationdemo1.fragment.FourFragment;
import com.example.another.bottomnavigationdemo1.fragment.SecondFragment;
import com.example.another.bottomnavigationdemo1.fragment.ThirdFragment;

/***
 * 参考博客：https://blog.csdn.net/jxq1994/article/details/52573506
 */
public class SecondActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {

    private Fragment mFragment1, mFragment2, mFragment3, mFragment4, mFragment5;
    private RadioGroup mRadioGroup;
    private RadioButton mRadioButton1, mRadioButton2, mRadioButton3, mRadioButton4, mRadioButton5;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        initViews();
    }

    private void initViews() {
        mRadioGroup = (RadioGroup) findViewById(R.id.radio_group);
        mRadioGroup.setOnCheckedChangeListener(this);

        mRadioButton1 = (RadioButton) findViewById(R.id.radio1);
        mRadioButton2 = (RadioButton) findViewById(R.id.radio2);
        mRadioButton3 = (RadioButton) findViewById(R.id.radio3);
        mRadioButton4 = (RadioButton) findViewById(R.id.radio4);
        mRadioButton5 = (RadioButton) findViewById(R.id.radio5);

        mRadioButton1.setChecked(true);

    }

    private void initFragments(FragmentTransaction fragmentTransaction) {
        if (mFragment1 != null) {
            fragmentTransaction.hide(mFragment1);
        }
        if (mFragment2 != null) {
            fragmentTransaction.hide(mFragment2);
        }

        if (mFragment3 != null) {
            fragmentTransaction.hide(mFragment3);
        }

        if (mFragment4 != null) {
            fragmentTransaction.hide(mFragment4);
        }

        if (mFragment5 != null) {
            fragmentTransaction.hide(mFragment5);
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {

        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        initFragments(fragmentTransaction);
        switch (checkedId) {
            case R.id.radio1:
                if (mFragment1 == null) {
                    mFragment1 = new FirstFragment();
                    fragmentTransaction.add(R.id.fragment_container, mFragment1);
                } else {
                    fragmentTransaction.show(mFragment1);
                }

                break;
            case R.id.radio2:

                if (mFragment2 == null) {
                    mFragment2 = new SecondFragment();
                    fragmentTransaction.add(R.id.fragment_container, mFragment2);
                } else {
                    fragmentTransaction.show(mFragment2);
                }

                break;
            case R.id.radio3:

                if (mFragment3 == null) {
                    mFragment3 = new ThirdFragment();
                    fragmentTransaction.add(R.id.fragment_container, mFragment3);
                } else {
                    fragmentTransaction.show(mFragment3);
                }

                break;
            case R.id.radio4:

                if (mFragment4 == null) {
                    mFragment4 = new FourFragment();
                    fragmentTransaction.add(R.id.fragment_container, mFragment4);
                } else {
                    fragmentTransaction.show(mFragment4);
                }

                break;
            case R.id.radio5:

                if (mFragment5 == null) {
                    mFragment5 = new FiveFragment();
                    fragmentTransaction.add(R.id.fragment_container, mFragment5);
                } else {
                    fragmentTransaction.show(mFragment5);
                }
                break;
            default:
                break;
        }
        fragmentTransaction.commit();
    }
}
